package com.yfq.springframework.context.annotation;

import cn.hutool.core.util.ClassUtil;
import com.yfq.springframework.beans.config.BeanDefinition;
import com.yfq.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 姚凤桥
 * @date 2023/2/4 21:07
 * @description 包扫描器，扫描指定包下的所有BeanDefinitions
 */
public class ClassPathScanningCandidateComponentProvider {

    protected Set<BeanDefinition> findCandidateComponents(String basePackage) {
        Set<BeanDefinition> beanDefinitions = new HashSet<>();
        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackage, Component.class);

        if (classes != null) {
            for (Class<?> aClass : classes) {
                beanDefinitions.add(new BeanDefinition(aClass));
            }
        }

        return beanDefinitions;
    }
}
